package com.financing.platform.task;

import com.financing.platform.dto.FaUserDTO;
import com.financing.platform.service.IFaUserService;
import com.financing.platform.service.IOrderService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 
* @ClassName: FaVerifyMailJob 
* @Description: 
* @author chenming 
* @date 2014年9月22日 下午6:32:23 
*
 */
@Component(value="faUserSystemRateJob" )
public class FaUserSystemRateJob {
	
	Logger log = Logger.getLogger(this.getClass());

	@Autowired
	IOrderService orderServiceImpl;

	@Autowired
	IFaUserService faUserServiceImpl;


	private ExecutorService es = Executors.newFixedThreadPool(5);
	private static boolean runTimeFlag = false;

	@Value("#{settings['userSystemRateFlag']}")
	private Boolean userSystemRateFlag;


	@Scheduled(cron = "0 0 02 * * ?") //发布  凌晨2点获取数据
	//@Scheduled(cron = "0 0/5 * * * ?") //测试 每5分钟更新一次数据
	public void faVerifyNeed(){

		// flag
		if(!userSystemRateFlag){
			return ;
		}

		log.info("FaUserSystemRateJob run Begin");

		if (runTimeFlag) {
			return;
		} else {
			runTimeFlag = true;
		}

		try{

			//获取已经成功订单的理财师
			List<String> list = orderServiceImpl.queryFaUserOrder();
			HashMap<String,String> map = new  HashMap<String,String>();
			for (String faid:list){
				map.put(faid,faid);
			}

			List<FaUserDTO> faList = null;
			FaUserDTO faUserDTO = new FaUserDTO();
			faUserDTO.setPageSize(5000);
 			int count = faUserServiceImpl.queryAllFaUserSystemRateCount(faUserDTO);


			//如果查询总数量 《= PageSize，直接处理
			if(count <= faUserDTO.getPageSize()){
				log.info("Begin faUserCsv list《=!"+count+":"+faUserDTO.getPageSize());
				faList = faUserServiceImpl.queryAllFaUserSystemRate(faUserDTO);
				userHandle(map,faList);
			}else{
				//如果查询总数量  > PageSize，
				int i = 1;
				do {
					faUserDTO.setPageNo(i++);
					faList = faUserServiceImpl.queryAllFaUserSystemRate(faUserDTO);

					final  HashMap<String,String> finalMap = map;
					final  List<FaUserDTO> finalFaList = faList;

					es.execute(new Runnable() {
						@Override
						public void run() {
							try {
								userHandle(finalMap,finalFaList);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
					Thread.sleep(3000L);
				} while (faList.size()>0 && faList.size() == faUserDTO.getPageSize());
			}


		}catch(Exception e){
			log.error("FaUserSystemRateJob run error",e);
		}
		runTimeFlag = false;
		log.info("FaUserSystemRateJob run end");
		
	}

	private void userHandle(HashMap<String,String> map,List<FaUserDTO> faList) throws Exception {
		if(faList == null || faList.size()<=0){
			return ;
		}
		for (FaUserDTO faUserDTO:faList){
			//OK
			if(map.containsKey(faUserDTO.getId().toString()) ){
				if( !"OK".equals(faUserDTO.getuSystemRate())) {
					faUserDTO.setuSystemRate("OK");
					faUserServiceImpl.updateFaUserSystemRate(faUserDTO);
					log.info("FaUserSystemRateJob OK:userid=" + faUserDTO.getId().toString());
				}
				continue;
			}
			Integer loginNum = faUserDTO.getLoginNum()==null?0:faUserDTO.getLoginNum();

			if(loginNum > 5 &&  !"A".equals(faUserDTO.getuSystemRate())){
				faUserDTO.setuSystemRate("A");
				faUserServiceImpl.updateFaUserSystemRate(faUserDTO);
				//log.info("FaUserSystemRateJob A:userid="+faUserDTO.getId().toString());
			}else if((loginNum >= 3 && loginNum <= 5 ) &&  !"B".equals(faUserDTO.getuSystemRate())){
				faUserDTO.setuSystemRate("B");
				faUserServiceImpl.updateFaUserSystemRate(faUserDTO);
				//log.info("FaUserSystemRateJob B:userid="+faUserDTO.getId().toString());
			}else if((loginNum >= 1 && loginNum <= 2 ) &&  !"C".equals(faUserDTO.getuSystemRate())){
				faUserDTO.setuSystemRate("C");
				faUserServiceImpl.updateFaUserSystemRate(faUserDTO);
				//log.info("FaUserSystemRateJob B:userid="+faUserDTO.getId().toString());
			}else if((loginNum <1 ) &&  !"D".equals(faUserDTO.getuSystemRate())){
				faUserDTO.setuSystemRate("D");
				faUserServiceImpl.updateFaUserSystemRate(faUserDTO);
				//log.info("FaUserSystemRateJob B:userid="+faUserDTO.getId().toString());
			}else{
				//log.info("FaUserSystemRateJob: "+faUserDTO.getuSystemRate()+":userid="+faUserDTO.getId().toString());
			}

		}
	}
	
}
